#include "simplekeyboard.h"
#include "touchkeyboard.h"
#include <QGridLayout>
#include <QApplication>

SimpleKeyboard::SimpleKeyboard(QWidget *parent)
    : TouchKeyboard(parent)
{
    // 构造函数会自动调用父类的初始化
    // 现在在派生类构造完成后调用setMode，确保虚函数表已经正确设置
    setMode(SimpleMode);
}

void SimpleKeyboard::createSimpleLayout()
{
    // 声明变量在函数作用域
    int baseButtonSize = 0;
    int spacing = 0;
    int buttonWidth = 0;
    int buttonHeight = 0;
    
    // 获取动态计算的尺寸
    baseButtonSize = calculateButtonSize();
    
    // 检查计算结果是否有效
    if (baseButtonSize <= 0) {
        return;
    }
    
    // 统一按键尺寸：将按钮尺寸放大一倍
    buttonWidth = static_cast<int>(baseButtonSize * 2.0);
    buttonHeight = buttonWidth + 10; // 增加10px高度
    
    // 自动调整间隙：约为按钮尺寸的1/10，但最小为5px，最大为15px
    spacing = qBound(5, buttonHeight / 10, 15);
    
    // 再次检查最终尺寸
    if (buttonWidth <= 0 || buttonHeight <= 0) {
        return;
    }
    
    // 优化：重用现有布局如果可能
    QLayout *existingLayout = m_keyboardArea->layout();
    QGridLayout *mainLayout = nullptr;
    
    if (existingLayout) {
        mainLayout = qobject_cast<QGridLayout*>(existingLayout);
        if (!mainLayout) {
            // 如果现有布局不是网格布局，删除它
            delete existingLayout;
            mainLayout = nullptr;
        } else {
            // 清空现有布局中的控件但不删除它们
            QLayoutItem *item;
            while ((item = mainLayout->takeAt(0)) != nullptr) {
                if (item->widget()) {
                    item->widget()->hide();
                }
                delete item;
            }
        }
    }
    
    // 如果没有现有布局或删除了现有布局，创建新的
    if (!mainLayout) {
        mainLayout = new QGridLayout(m_keyboardArea);
        m_keyboardArea->setLayout(mainLayout);
    }
    
    mainLayout->setSpacing(spacing);
    mainLayout->setContentsMargins(spacing * 2, spacing * 2, spacing * 2, spacing * 2);
    
    // 设置行列的最小尺寸，确保所有按钮尺寸一致
    for (int i = 0; i < 4; i++) { // 4行
        mainLayout->setRowMinimumHeight(i, buttonHeight);
        mainLayout->setRowStretch(i, 0);
    }
    for (int i = 0; i < 4; i++) { // 4列
        mainLayout->setColumnMinimumWidth(i, buttonWidth);
        mainLayout->setColumnStretch(i, 0);
    }
    
    // 设置布局居中对齐
    mainLayout->setAlignment(Qt::AlignCenter);
    
    // 创建所有按钮并确保尺寸一致
    // 第一行：数字1, 2, 3 + 退格键
    KeyboardButton *btn1 = new KeyboardButton("1", "1");
    btn1->setFixedSize(buttonWidth, buttonHeight);
    btn1->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(btn1, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    mainLayout->addWidget(btn1, 0, 0);
    
    KeyboardButton *btn2 = new KeyboardButton("2", "2");
    btn2->setFixedSize(buttonWidth, buttonHeight);
    btn2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(btn2, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    mainLayout->addWidget(btn2, 0, 1);
    
    KeyboardButton *btn3 = new KeyboardButton("3", "3");
    btn3->setFixedSize(buttonWidth, buttonHeight);
    btn3->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(btn3, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    mainLayout->addWidget(btn3, 0, 2);
    
    KeyboardButton *backspaceBtn = new KeyboardButton("⌫", "backspace");
    backspaceBtn->setFunctionKey(true);
    backspaceBtn->setFixedSize(buttonWidth, buttonHeight);
    backspaceBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(backspaceBtn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    mainLayout->addWidget(backspaceBtn, 0, 3);
    
    // 第二行：数字4, 5, 6 + 清除键
    KeyboardButton *btn4 = new KeyboardButton("4", "4");
    btn4->setFixedSize(buttonWidth, buttonHeight);
    btn4->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(btn4, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    mainLayout->addWidget(btn4, 1, 0);
    
    KeyboardButton *btn5 = new KeyboardButton("5", "5");
    btn5->setFixedSize(buttonWidth, buttonHeight);
    btn5->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(btn5, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    mainLayout->addWidget(btn5, 1, 1);
    
    KeyboardButton *btn6 = new KeyboardButton("6", "6");
    btn6->setFixedSize(buttonWidth, buttonHeight);
    btn6->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(btn6, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    mainLayout->addWidget(btn6, 1, 2);
    
    KeyboardButton *clearBtn = new KeyboardButton("清除", "clear_all");
    clearBtn->setFunctionKey(true);
    clearBtn->setFixedSize(buttonWidth, buttonHeight);
    clearBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(clearBtn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    mainLayout->addWidget(clearBtn, 1, 3);
    
    // 第三行：数字7, 8, 9 + 回车键（占据第三行和第四行最后位置）
    KeyboardButton *btn7 = new KeyboardButton("7", "7");
    btn7->setFixedSize(buttonWidth, buttonHeight);
    btn7->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(btn7, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    mainLayout->addWidget(btn7, 2, 0);
    
    KeyboardButton *btn8 = new KeyboardButton("8", "8");
    btn8->setFixedSize(buttonWidth, buttonHeight);
    btn8->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(btn8, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    mainLayout->addWidget(btn8, 2, 1);
    
    KeyboardButton *btn9 = new KeyboardButton("9", "9");
    btn9->setFixedSize(buttonWidth, buttonHeight);
    btn9->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(btn9, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    mainLayout->addWidget(btn9, 2, 2);
    
    // 回车键：占据第三行和第四行最后位置（使用行跨度）
    KeyboardButton *enterBtn = new KeyboardButton("↵", "enter");
    m_enterButton = enterBtn; // 保存回车键引用
    enterBtn->setFunctionKey(true);
    enterBtn->setFixedSize(buttonWidth, buttonHeight * 2 + spacing); // 高度为两个按钮加间距
    enterBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(enterBtn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    mainLayout->addWidget(enterBtn, 2, 3, 2, 1); // 从第3行开始，跨越2行，占1列
    
    // 第四行：模式切换键, 数字0, 小数点
    KeyboardButton *modeBtn = new KeyboardButton("⌨", "mode_toggle");
    modeBtn->setFunctionKey(true);
    modeBtn->setFixedSize(buttonWidth, buttonHeight);
    modeBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(modeBtn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onModeToggle);
    mainLayout->addWidget(modeBtn, 3, 0);
    
    KeyboardButton *btn0 = new KeyboardButton("0", "0");
    btn0->setFixedSize(buttonWidth, buttonHeight);
    btn0->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(btn0, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    mainLayout->addWidget(btn0, 3, 1);
    
    KeyboardButton *dotBtn = new KeyboardButton(".", ".");
    dotBtn->setFixedSize(buttonWidth, buttonHeight);
    dotBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(dotBtn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    mainLayout->addWidget(dotBtn, 3, 2);
    
    // 让键盘区域根据内容自动调整尺寸
    m_keyboardArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    
    // 显示键盘区域
    m_keyboardArea->show();
}

void SimpleKeyboard::createFullLayout()
{
    // 获取动态计算的尺寸
    int originalButtonSize = calculateButtonSize();
    int originalSpacing = calculateSpacing();
    
    // 适配当前键盘尺寸，全键盘模式下按钮缩小两倍（原来是缩小一倍，现在再缩小一倍）
    int buttonSize = originalButtonSize / 4;
    int spacing = originalSpacing / 4;
    
    // 确保最小按钮尺寸
    buttonSize = qMax(buttonSize, 25);
    spacing = qMax(spacing, 1);
    
    // 全键盘模式布局 - 使用绝对定位，可以随意调整位置
    // 注意：布局清理已经在 setMode() 中完成，这里直接创建
    
    // 计算各行实际宽度（考虑末尾按钮宽度增加）
    // 第1行：10个数字按钮 + 退格键(2.5倍宽度)
    int row1Width = 10 * buttonSize + 9 * spacing + (buttonSize * 2.5 + spacing * 1.5);
    
    // 第2行：10个字母按钮(左移0.5个按钮宽度) + 键盘图标(2倍宽度)
    int row2Width = 10 * buttonSize + 9 * spacing + (buttonSize * 2 + spacing) + buttonSize / 2;
    
    // 第3行：复制键盘图标(1倍) + 9个字母按钮 + 确认键(2.5倍宽度)
    int row3Width = (buttonSize * 1.0) + spacing + 9 * buttonSize + 8 * spacing + (buttonSize * 2.5 + spacing * 1.5);
    
    // 第4行：大小写切换(1.5倍) + 9个按钮 + 大小写切换(2倍宽度)
    int row4Width = (buttonSize * 1.5 + spacing * 0.5) + spacing + 9 * buttonSize + 8 * spacing + (buttonSize * 2 + spacing);
    
    // 第5行：< 🌐 空格 清除 >（5个按钮）
    int row5Width = (buttonSize * 1.5) + spacing + (buttonSize * 1.5) + spacing + (buttonSize * 6.5 + spacing * 5) + spacing + (buttonSize * 1.5 + spacing * 0.5) + spacing + (buttonSize * 1.5);
    
    // 取所有行中的最大宽度
    int maxRowWidth = qMax(qMax(qMax(qMax(row1Width, row2Width), row3Width), row4Width), row5Width);
    
    // 获取标题栏宽度（键盘总宽度减去边距）
    int headerWidth = width() - 4;  // 减去左右2px边距
    
    // 计算容器尺寸，确保能容纳所有按键并有适当边距
    // 现在让键盘区域宽度与标题栏保持一致，并减少20px
    int totalWidth = headerWidth - 20;  // 与标题栏宽度一致并减少20px
    int totalHeight = buttonSize * 5 + spacing * 4 + 40;   // 5行高度
    
    // 设置键盘区域尺寸
    m_keyboardArea->setFixedSize(totalWidth, totalHeight);
    
    // 计算内容区域尺寸
    int contentHeight = buttonSize * 5 + spacing * 4; // 实际按键内容高度
    int contentWidth = maxRowWidth; // 实际按键内容宽度
    
    // 计算居中偏移量
    int horizontalOffset = (totalWidth - contentWidth) / 2; // 水平居中偏移
    int verticalOffset = (totalHeight - contentHeight) / 2; // 垂直居中偏移
    
    // 起始坐标（居中后再左移20px，取代之前的右移40px）
    int x = horizontalOffset - 20; // 居中后再左移20px
    int y = verticalOffset;
    
    // 第一行：1 2 3 4 5 6 7 8 9 0 ⌫（数字行）
    QStringList numbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
    for (int i = 0; i < numbers.size(); ++i) {
        KeyboardButton *btn = new KeyboardButton(numbers[i], numbers[i], m_keyboardArea);
        btn->setFixedSize(buttonSize, buttonSize);
        btn->move(x + i * (buttonSize + spacing), y);
        connect(btn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
        btn->show();
    }
    
    // 退格键（第一行最后，宽度为2.5倍，原2倍加0.5倍）
    KeyboardButton *backspaceBtn = new KeyboardButton("⌫", "backspace", m_keyboardArea);
    backspaceBtn->setFunctionKey(true);
    backspaceBtn->setFixedSize(buttonSize * 2.5 + spacing * 1.5, buttonSize);
    backspaceBtn->move(x + 10 * (buttonSize + spacing), y);
    connect(backspaceBtn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    backspaceBtn->show();
    
    // 第二行：q w e r t y u i o p（左移半个按钮宽度）
    y += buttonSize + spacing;
    int row2OffsetX = buttonSize / 2; // 左移半个按钮宽度
    QStringList row1 = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p"};
    for (int i = 0; i < row1.size(); ++i) {
        // 根据大小写状态显示字母
        QString displayText = m_isCapitalized ? row1[i].toUpper() : row1[i];
        KeyboardButton *btn = new KeyboardButton(displayText, row1[i], m_keyboardArea);
        btn->setFixedSize(buttonSize, buttonSize);
        btn->move(x + row2OffsetX + i * (buttonSize + spacing), y);
        connect(btn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
        btn->show();
    }
    
    // 键盘图标（模式切换，从第5行移动到第2行末尾，2倍宽度，原1.5倍加0.5倍）
    KeyboardButton *keyboardBtn = new KeyboardButton("⌨", "keyboard_switch", m_keyboardArea);
    keyboardBtn->setFunctionKey(true);
    keyboardBtn->setFixedSize(buttonSize * 2 + spacing, buttonSize);
    keyboardBtn->move(x + row2OffsetX + 10 * (buttonSize + spacing), y);
    connect(keyboardBtn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onModeToggle);
    keyboardBtn->show();
    
    // 第三行：复制的键盘图标按钮 + a s d f g h j k l 确认键
    y += buttonSize + spacing;
    
    // 复制的键盘图标按钮（从第2行最后一个复制而来，放在第3行开头，宽度减少0.5倍）
    KeyboardButton *keyboardBtn2 = new KeyboardButton("⌨", "keyboard_switch", m_keyboardArea);
    keyboardBtn2->setFunctionKey(true);
    keyboardBtn2->setFixedSize(buttonSize * 1.0, buttonSize);
    keyboardBtn2->move(x, y); // 位于第三行最左侧
    connect(keyboardBtn2, &KeyboardButton::keyPressed, this, &TouchKeyboard::onModeToggle);
    keyboardBtn2->show();
    
    // 计算字母按钮的起始位置（在复制的键盘按钮之后）
    int keyboardBtn2Width = buttonSize * 1.0;
    int letterStartX = x + keyboardBtn2Width + spacing;
    
    QStringList row2 = {"a", "s", "d", "f", "g", "h", "j", "k", "l"};
    for (int i = 0; i < row2.size(); ++i) {
        // 根据大小写状态显示字母
        QString displayText = m_isCapitalized ? row2[i].toUpper() : row2[i];
        KeyboardButton *btn = new KeyboardButton(displayText, row2[i], m_keyboardArea);
        btn->setFixedSize(buttonSize, buttonSize);
        btn->move(letterStartX + i * (buttonSize + spacing), y);
        connect(btn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
        btn->show();
    }
    
    // 确认键（第三行最右侧，宽度增加0.5倍前一个按钮宽度，使用回车图标）
    KeyboardButton *enterBtn = new KeyboardButton("↵", "enter", m_keyboardArea);
    m_enterButton = enterBtn; // 保存回车键引用
    enterBtn->setFunctionKey(true);
    enterBtn->setFixedSize(buttonSize * 2.5 + spacing * 1.5, buttonSize); // 增加0.5倍前一个按钮宽度
    enterBtn->move(letterStartX + 9 * (buttonSize + spacing), y); // 使用新的计算基准
    connect(enterBtn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    enterBtn->show();
    
    // 第四行：隐藏键（复制） z x c v b n m , . 隐藏键
    y += buttonSize + spacing;
    
    // 隐藏键（第四行最左侧，改为大小写切换功能，宽度为1.5倍）
    // 根据当前大小写状态显示不同的箭头
    QString shiftText2 = m_isCapitalized ? "⇧" : "↑";
    KeyboardButton *shiftBtn2 = new KeyboardButton(shiftText2, "shift", m_keyboardArea);
    shiftBtn2->setFunctionKey(true);
    shiftBtn2->setFixedSize(buttonSize * 1.5 + spacing * 0.5, buttonSize);
    shiftBtn2->move(x, y); // 位于第四行最左侧
    connect(shiftBtn2, &KeyboardButton::keyPressed, this, &TouchKeyboard::onShiftToggle);
    shiftBtn2->show();
    
    // 计算第2个按钮（z）的位置，与第1个按钮间距与后续按钮间距一致
    int firstBtnWidth = buttonSize * 1.5 + spacing * 0.5; // 第1个按钮宽度
    int secondBtnStartX = x + firstBtnWidth + spacing; // 第2个按钮起始位置，与第1个按钮间距为spacing
    
    QStringList row3 = {"z", "x", "c", "v", "b", "n", "m", ",", "."};
    for (int i = 0; i < row3.size(); ++i) {
        // 只对字母应用大小写，标点符号保持原样
        QString displayText = row3[i];
        if (row3[i].length() == 1 && row3[i].at(0).isLetter()) {
            displayText = m_isCapitalized ? row3[i].toUpper() : row3[i];
        }
        KeyboardButton *btn = new KeyboardButton(displayText, row3[i], m_keyboardArea);
        btn->setFixedSize(buttonSize, buttonSize);
        btn->move(secondBtnStartX + i * (buttonSize + spacing), y); // 使用新的起始位置
        connect(btn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
        btn->show();
    }
    
    // 大小写切换键（第四行最右侧，原隐藏键改为大小写切换，宽度为2倍，原1.5倍加0.5倍）
    // 根据当前大小写状态显示不同的箭头
    QString shiftText = m_isCapitalized ? "⇧" : "↑";
    KeyboardButton *shiftBtn = new KeyboardButton(shiftText, "shift", m_keyboardArea);
    shiftBtn->setFunctionKey(true);
    shiftBtn->setFixedSize(buttonSize * 2 + spacing, buttonSize);
    shiftBtn->move(secondBtnStartX + 9 * (buttonSize + spacing), y); // 使用新的计算基准
    connect(shiftBtn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onShiftToggle);
    shiftBtn->show();
    
    // 第五行：< 🌐 空格 清除 >（添加中文翻页键）
    y += buttonSize + spacing;

    // 第5行按钮重新设计：前翻页、地球图标、空格按钮、清除按钮、后翻页
    int prevPageWidth = buttonSize * 1.5; // <按钮增加0.5倍宽度
    int langBtnWidth = buttonSize * 1.5; // 🌐按钮减少0.5倍宽度（原2倍改为1.5倍）
    int spaceBtnWidth = buttonSize * 6.5 + spacing * 5; // 空格按钮增加0.5倍宽度（从6倍改为6.5倍）
    int clearBtnWidth = buttonSize * 1.5 + spacing * 0.5; // 清除按钮1.5倍宽度
    int nextPageWidth = buttonSize * 1.5; // >按钮增加0.5倍宽度
    
    // 计算按钮位置（第5行第1个按钮与第4行第1个按钮左对齐）
    int prevPageX = x; // <按钮（第1个，与第4行第1个按钮左对齐）
    int langBtnX = prevPageX + prevPageWidth + spacing; // 🌐按钮
    int spaceBtnX = langBtnX + langBtnWidth + spacing; // 空格按钮
    int clearBtnX = spaceBtnX + spaceBtnWidth + spacing; // 清除按钮
    int nextPageX = clearBtnX + clearBtnWidth + spacing; // >按钮

    // 前翻页按钮（<）
    KeyboardButton *prevPageBtn = new KeyboardButton("<", "<", m_keyboardArea);
    prevPageBtn->setFunctionKey(true);
    prevPageBtn->setFixedSize(prevPageWidth, buttonSize);
    prevPageBtn->move(prevPageX, y);
    connect(prevPageBtn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    prevPageBtn->show();
    
    // 地球图标（语言切换）
    m_languageButton = new KeyboardButton("En", "language", m_keyboardArea);
    m_languageButton->setFunctionKey(true);
    m_languageButton->setFixedSize(langBtnWidth, buttonSize);
    m_languageButton->move(langBtnX, y);
    // 启用中英文切换功能
    connect(m_languageButton, &KeyboardButton::keyPressed, this, &TouchKeyboard::onLanguageToggle);
    m_languageButton->show();
    
    // 空格按键
    KeyboardButton *spaceBtn = new KeyboardButton("空格", " ", m_keyboardArea);
    spaceBtn->setFunctionKey(true);
    spaceBtn->setFixedSize(spaceBtnWidth, buttonSize);
    // 设置较小的字体大小
    QFont font = spaceBtn->font();
    font.setPointSize(10); // 设置为10pt字体
    spaceBtn->setFont(font);
    spaceBtn->move(spaceBtnX, y);
    connect(spaceBtn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    spaceBtn->show();
    
    // 清除按钮
    KeyboardButton *clearBtn = new KeyboardButton("清除", "clear_all", m_keyboardArea);
    clearBtn->setFunctionKey(true);
    clearBtn->setFixedSize(clearBtnWidth, buttonSize);
    // 设置较小的字体大小
    QFont clearFont = clearBtn->font();
    clearFont.setPointSize(10); // 设置为10pt字体
    clearBtn->setFont(clearFont);
    clearBtn->move(clearBtnX, y);
    connect(clearBtn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    clearBtn->show();
    
    // 后翻页按钮（>）
    KeyboardButton *nextPageBtn = new KeyboardButton(">", ">", m_keyboardArea);
    nextPageBtn->setFunctionKey(true);
    nextPageBtn->setFixedSize(nextPageWidth, buttonSize);
    nextPageBtn->move(nextPageX, y);
    connect(nextPageBtn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    nextPageBtn->show();
}

void SimpleKeyboard::addNumberRow(QGridLayout *layout, int row, const QStringList &numbers)
{
    for (int i = 0; i < numbers.size(); ++i) {
        KeyboardButton *btn = new KeyboardButton(numbers[i], numbers[i]);
        connect(btn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
        layout->addWidget(btn, row, i);
    }
}

void SimpleKeyboard::addNumberButtonToLayout(QGridLayout *layout, int row, int col, const QString &text, int customSize)
{
    KeyboardButton *btn = new KeyboardButton(text, text);
    // 为数字按钮设置固定尺寸，确保宽高一致
    btn->setFixedSize(customSize, customSize);
    btn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(btn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    layout->addWidget(btn, row, col);
}

void SimpleKeyboard::addNumberButtonToLayout(QGridLayout *layout, int row, int col, const QString &text)
{
    // 这个重载函数不使用自定义尺寸，让布局系统自动处理
    KeyboardButton *btn = new KeyboardButton(text, text);
    btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    connect(btn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    layout->addWidget(btn, row, col);
}